package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.SOrderImportedStowageService;
import cn.huiyunche.base.service.mappers.SOrderImportedStowageMapper;
import cn.huiyunche.base.service.model.SOrderImportedStowage;
import cn.huiyunche.base.service.model.SOrderImportedStowageExample;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SOrderImportedStowageImpl implements SOrderImportedStowageService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SOrderImportedStowageImpl.class);

    @Autowired
    private SOrderImportedStowageMapper sOrderImportedStowageMapper;

    @Override
    public Long add(Long importedOrderId, Long stowageModelId) {
        LOGGER.info("add params : {}, {}", importedOrderId, stowageModelId);
        if (null == importedOrderId) {
            LOGGER.error("add importedOrderId must not be null");
            throw new IllegalArgumentException("导入订单id不能为空");
        }
        if (null == stowageModelId) {
            LOGGER.error("add stowageModelId must not be null");
            throw new IllegalArgumentException("配置模型id不能为空");
        }

        SOrderImportedStowage orderStowage = new SOrderImportedStowage();
        orderStowage.setImportedOrderId(importedOrderId);
        orderStowage.setStowageModelId(stowageModelId);

        sOrderImportedStowageMapper.insertSelective(orderStowage);
        return orderStowage.getId();
    }

    @Override
    public SOrderImportedStowage getByImportedOrderId(Long importedOrderId) {
        LOGGER.info("add param : {}", importedOrderId);
        if (null == importedOrderId) {
            LOGGER.error("add importedOrderId must not be null");
            throw new IllegalArgumentException("导入订单id不能为空");
        }

        SOrderImportedStowageExample example = new SOrderImportedStowageExample();
        example.createCriteria().andImportedOrderIdEqualTo(importedOrderId);
        List<SOrderImportedStowage> list = sOrderImportedStowageMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

}
